<?
include('../controller/handler.php');
include('../controller/twitwords.php');
include('../controller/twitter.php');
include('../scaffold/user.php');
include('../scaffold/tag.php');

$hd = new Handler();
$tw = new Twitwords();
$hd->openDB();
$base_url = $hd->getBaseURL();
$user = $tw->loadUserByName($_GET['id']);
$page = (isset($_GET['page']) && $_GET['page']>1)? $_GET['page'] : 1;
/* DEBUG */
//echo var_dump($_GET['page']);
if($user==NULL) {
	$user = new User("", "", "", $base_url."style/img/error.png", "", "");
	$tags = array();
	$class = "class=\"error\"";
	$stripe = "<div id=\"stripe\"></div>";
} else {
	if($hd->hasHTTPError($user->getImage())) {
		$user = $tw->updateProfile($user->getUserId(), $hd);
	}
	$quantity = $hd->getTagsPerPage();
	$offset = ($page-1)*$quantity;
	$tags = $tw->loadTags($user, $offset, $quantity);
	$tags_number = $tw->countTagOfUser($user->getUserId());
}
$hd->closeDB();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<link rel="alternate" type="application/rss+xml" href="<?echo $base_url;?>rss/<?echo $user->getUsername();?>" />
	<?
	/****************
	 * HEAD MODULE
	 ***************/
	include('../module/head.inc.php');
	?>

	<title><?echo $hd->getTitle();?> - 
	<?if($user->getUsername()!="") {
		echo $user->getUsername();
	} else {
		?>Unknown user <?echo $_GET['id'];
	}?></title>
	
</head>

<body>
<?
/****************
 * HEADER MODULE
 ***************/
include('../module/header.inc.php');
?>
<?echo $stripe;?>
<div id="container">
	<?
	/****************
	 * SEARCH MODULE
	 ***************/
	include('../module/search.inc.php');
	?>
	<h1 id="username">
		<img id="avatar" <?echo $class;?> src="<?echo $user->getImage();?>" alt="Profile image" />
		
		<?if($user->getUsername()!="") {
			echo $user->getUsername();
		} else {
			?>Unknown user<?
		}?>
	</h1>
	<div id="user">
	<img id="logo" src="<?echo $base_url;?>style/img/twitter.png" alt="Twitter logo" />
	
	<p><?if($user->getName()!="") { 
			echo $user->getName();
		} else {
			?>real name not specified<?
		}
		if($user->getLocation()!="") {
			?> @ <?echo $user->getLocation();
		}?></p>
	<p><a href="http://twitter.com/<?echo $user->getUsername();?>" target="_blank">
		http://twitter.com/<?echo $user->getUsername();?>
	</a></p>
	<p><?if($user->getURL()!="") {
			?><a href="<?echo $user->getURL();?>" target="_blank"><?echo $user->getURL();?></a>
		<?} else {
			?>personal site not specified<?
		}?></p>
	</div>
	<?if(isset($_GET['cloud'])) {?>
		<div id="cloud">
		<?foreach($tags as $tag) {?>
		<a class="priority-<?echo $tag->getPriority();?>" href="<?echo $hd->getBaseURL();?>search/tag:<?echo urlencode($tag->getTag());?>">
			<?echo $tag->getTag();?>
		</a>
		<?}?>
		</div>
	<?} else {
		$i = $offset;
		foreach($tags as $tag) {
			$i++;
			$style = ($i%2==0)? "even" : "odd";
			?>
			<div class="tag <?echo $style;?>">
			<p class="text"><?echo $tag->getTag();?>
			<a class="twit" href="http://twitter.com/<?echo $user->getUsername();?>/statuses/<?echo $tag->getStatus();?>" target="_blank">show twit</a></p>
			<p class="date"><?echo $hd->deltaToTime(time(), strtotime($tag->getdate()));?></p>
			</div>
			<?
		}
	}?>

	<div id="credits"><?echo $hd->getCredits();?></div>
</div>
<div id="footer">
	<div id="nav">
	<?if($page>1) {?>
	<a href="<?echo $base_url?>user/<?echo $user->getUsername();?>/page:<?echo $page-1;?>">&larr; newer</a>
	<?} if($user->getUsername()!="") {?>
		<?if(isset($_GET['cloud'])) {?>
	<a href="<?echo $base_url?>user/<?echo $user->getUsername();?>">{ list }</a>
		<?} else {?>
	<a href="<?echo $base_url?>user/<?echo $user->getUsername();?>:cloud">{ cloud }</a>
		<?}?>
	{ <?echo $tags_number;?> tags }
	<a href="<?echo "feed://".$_SERVER['SERVER_NAME'].$base_url?>rss/<?echo $user->getUsername();?>">{ rss }</a>
	<?}	if($tags_number>$offset+$quantity && !isset($_GET['cloud'])) {?>
	<a href="<?echo $base_url?>user/<?echo $user->getUsername();?>/page:<?echo $page+1;?>">older &rarr;</a>
	<?}?>
	</div>
	<?
	/****************
	 * FOOTER MODULE
	 ***************/
	include('../module/footer.inc.php');
	?>
</div>
<?include('../module/analytics.inc.php');?>
</body>
</html>